package com.zeng.vuerestful.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.vueserve.coreapi.vo.AuthorityVo;
import com.zeng.vueserve.coreapi.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/authority")
@RestController
public class AuthorityController {

    @Autowired
    AuthorityService authorityService;

    @RequestMapping("/list")
    public Object list(Page page, AuthorityVo authorityVo) {
        List<AuthorityVo> page1 = this.authorityService.findPage(page, authorityVo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
                    .put("name", e.getName())
                    .put("url", e.getUrl())
                    .put("detail", e.getDetail())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("authority", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("authority", authorityService.getOneByCode(code));

    }

    @RequestMapping("/add")
    public Object add(AuthorityVo authorityVo) {

        return ResponseUtil.success("操作成功").addData("authority", authorityService.addOne(authorityVo));

    }

    @RequestMapping("/update")
    public Object update(AuthorityVo authorityVo) {
        Integer update = this.authorityService.update(authorityVo);

        return ResponseUtil.success("操作成功")
                .addData("updateCount", update);
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        Integer delete = this.authorityService.delete(code);
        return ResponseUtil.success("操作成功").addData("deleteCount", delete);
    }
    @RequestMapping("/batchDelete")
    public Object batchDelete(List<String> codes) {
        Integer integer = this.authorityService.batchDelete(codes);

        return ResponseUtil.success("操作成功").addData("deleteCount", integer);
    }
}

